package nicg.st.util;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

import nicg.st.entity.LeafIndicator;

public class XmlParse {
	
	private static List<LeafIndicator> LeafIndicatorList = null;
	private static LeafIndicator LeafIndicator = null;
	
	public static List<LeafIndicator> getLeafIndicators(File file){
		
		SAXReader reader = new SAXReader();
		try {
			Document document = reader.read(file);
			Element LeafIndicatorstore = document.getRootElement();
			Iterator storeit = LeafIndicatorstore.elementIterator();
			
			LeafIndicatorList = new ArrayList<LeafIndicator>();
			while(storeit.hasNext()){
				
				LeafIndicator = new LeafIndicator();
				Element LeafIndicatorElement = (Element) storeit.next();
				//遍历LeafIndicatorElement的属性
				List<Attribute> attributes = LeafIndicatorElement.attributes();
				for(Attribute attribute : attributes){
					if(attribute.getName().equals("id")){
						String id = attribute.getValue();
						LeafIndicator.setId(Integer.parseInt(id));
					}
				}
				
				Iterator LeafIndicatorit = LeafIndicatorElement.elementIterator();
				while(LeafIndicatorit.hasNext()){
					Element child = (Element) LeafIndicatorit.next();
					String nodeName = child.getName();
					if(nodeName.equals("name")){
						String name = child.getStringValue();
						LeafIndicator.setName(name);
					}else if(nodeName.equals("value")){
						String price = child.getStringValue();
						LeafIndicator.setValue(Double.parseDouble(price));
					}else if(nodeName.equals("max")){
						String price2 = child.getStringValue();
						LeafIndicator.setMax(Double.parseDouble(price2));
					}
				}
				LeafIndicatorList.add(LeafIndicator);
				LeafIndicator = null;
			}
		} catch (DocumentException e) {
			e.printStackTrace();
		}
		return LeafIndicatorList;
		
	}
	public static void writeLeafIndicators(String fileName,List<LeafIndicator> leafs) {
		File file = new File(fileName);
		if(file.exists()) {
			file.delete();
		}
		Document document = getDocumentByList(leafs);
		writeleafIndicators(fileName, leafs);
	}
	public static Document getDocumentByList(List<LeafIndicator> leafIndicators) {
		Document document = DocumentHelper.createDocument();
		Element root = document.addElement( "demos" );
		for(int i = 0;i<leafIndicators.size();i++) {
			Element item = root.addElement("leafIndicator")
								.addAttribute("id", String.valueOf(leafIndicators.get(i).getId()));
			item.addElement("name").setText(leafIndicators.get(i).getName());
			item.addElement("value").setText(String.valueOf(leafIndicators.get(i).getValue()));
			item.addElement("max").setText(String.valueOf(leafIndicators.get(i).getMax()));
		}
		return document;
	}
	private static void writeleafIndicators(String fileName,List<LeafIndicator> leafIndicators) {
		File file = new File(fileName);
		
		//setDocument
		Document document = null;
		document = XmlParse.getDocumentByList(leafIndicators);
			//set
		OutputFormat format = OutputFormat.createPrettyPrint();
		format.setEncoding("UTF-8");
		
		XMLWriter xmlWriter = null;
		try {
			xmlWriter = new XMLWriter(new FileOutputStream(file),format);
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		xmlWriter.setEscapeText(false);
		try {
			xmlWriter.write(document);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally {
			if(xmlWriter != null) {
				try {
					xmlWriter.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}
}
